package com.klikmee.sms.core.base.util;

import java.util.Date;
import java.util.GregorianCalendar;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.apache.log4j.Logger;

public class CoreUtilities {
	
	static Logger logger = Logger.getLogger( CoreUtilities.class ); 
	
	/**
	 * @param message is text of message
	 * @return number of smses witch is need to send this message 
	 */
	public static int messageLengthInSMSes(String message)
	{
		if(message.length()<=160)
		{
			return 1;
		}
		else if (message.length()<=306)
		{
			return 2;
		} 
		else if (message.length()<=459)
		{
			return 3;
		}
		else if (message.length()<=612)
		{
			return 4;
		}
		else if (message.length()<=756)
		{
			return 5;
		}
		else {
			
			return 6;
		}
		
	}
	
	
	private static DatatypeFactory datatypeFactory;

    static {
        try {
            datatypeFactory = DatatypeFactory.newInstance();
        } catch (DatatypeConfigurationException ex) {
            logger.fatal( "Error initializing utils", ex );
        }
    }
    
    public static XMLGregorianCalendar convertToXMLGregorianCalendar(Date date){
    	GregorianCalendar gregorianCalendar = new GregorianCalendar();
    	gregorianCalendar.setTime( date );
    	XMLGregorianCalendar xmlGregCalendar = datatypeFactory.newXMLGregorianCalendar( gregorianCalendar );
    	
    	return xmlGregCalendar;
    }
}
